<template>
  <div class="addArticle">
    <el-header style="line-height:60px"><h2>课程添加</h2></el-header>
        <div class="card kjfs">
          <el-row>
            <el-col :span="24">
              <p class="title" style="padding:25px"><i class="fa fa-th-large fa-lg"></i>课程录入表数据</p>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="15" :offset="3">
              <el-form ref="form" :model="form"
                                  label-width="180px"
                                  @submit.native.prevent
                                  action="#">
                <el-form-item label="课程ID:">
                  <el-input v-model="form.lesson_id"></el-input>
                </el-form-item>
                <el-form-item label="课程号ID：">
                  <el-input v-model="form.lesson_code"></el-input>
                </el-form-item>
                <el-form-item label="课程名称：">
                  <el-input v-model="form.lesson"></el-input>
                </el-form-item>
                <el-form-item label="课程说明：">
                  <el-input v-model="form.info"></el-input>
                </el-form-item>
                <el-form-item label="课程其他备注信息：">
                  <el-input v-model="form.remark"></el-input>
                </el-form-item>
                <el-form-item label="课程可用状态(是否删除)：">
                  <el-input v-model="form.is_del"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="button" @click="onSubmit()">立即增加</el-button>
                  <el-button>取消</el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </div>
  </div>
</template>

<script>
import Markdown from '../../components/markdown/markdown-editor'
import initData from '../../markData.js'
export default {
  name: 'lessonEditor',
  data () {
    return {
      fileList: [],
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      article: {
        title: '',
        tag: '',
        top: false,
        content: {}
      },
      rules: {
        lesson_id: [
          {required: true, message: '课程ID不能为空'}
        ],
        lesson_code: [
          {required: true, message: '课程号ID不能为空'}
        ],
        lesson: [
          {required: true, message: '课程名称不能为空'}
        ],
        info: [
          {required: true, message: '课程说明不能为空'}
        ],
        remark: [
          {required: true, message: '课程其他不能为空'}
        ]
      },
      initData: initData,
      restaurants: [],
      form: {
        lesson_id: '',
        lesson_code: '',
        lesson: '',
        info: '',
        remark: '',
        id_del: ''
      }
    }
  },
  components: {Markdown},
  methods: {
    onSubmit (form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.$http.post('/api/singlechoiceadd', this.form
            //  this.qs.stringify(this.form),
            //  {headers:{
            //    'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'
            //  }}
          )
            .then(response => {
              this.$message({
                type: 'success',
                message: '创建成功'
              })
            }).catch(error => { console.log(error) })
        } else {
          return false
        }
      })
    },
    change () {
      console.log(arguments[0], this.lesson)
      this.article.content = arguments[0]
    },
    querySearch (queryString, cb) {
      var restaurants = this.restaurants
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter (queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    loadAll () {
      return [
        { 'value': 'vue' },
        { 'value': 'node.js' }
      ]
    },
    handleSelect (item) {
      console.log(item)
    },
    submitlessonEditor () {
      this.$axios.post('/api/lessonEditor', {
        data: this.article
      })
        .then(function (response) {
          console.log(response)
        })
        .catch(function (error) {
          console.log(error)
        })
    }
  },
  mounted () {
    this.restaurants = this.loadAll()
  }
}
</script>

<style lang="scss">
  .subBtn{
  width: 100px;
  margin: 0 auto;
}
</style>
